/*
 * FreeRTOS V202104.00
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */


 /******************************************************************************
 *
 * http://www.FreeRTOS.org/cli
 *
 ******************************************************************************/


/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* FreeRTOS+CLI includes. */
#include "FreeRTOS_CLI.h"

#include "cli-command.h"

#ifndef configINCLUDE_QUERY_HEAP_COMMAND
	#define configINCLUDE_QUERY_HEAP_COMMAND 1
#endif

/*
 * The function that registers the commands that are defined within this file.
 */
void vRegisterSampleCLICommands( void );

/*
 * Implements the command.
 */
static BaseType_t prvGwShowCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
static BaseType_t prvReadRegCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
static BaseType_t prvWriteRegCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
static BaseType_t prvSpiToolCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
static BaseType_t prvMdioReadCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
static BaseType_t prvMdioWriteCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );

/* GT101 command line command */
static const CLI_Command_Definition_t xGwShow =
{
	"show",
	"\r\nshow:\r\n -- show state in device\r\n",
	prvGwShowCommand, /* The function to run. */
	0
};

static const CLI_Command_Definition_t xReadReg =
{
	"reg_read",
	"\r\nreg_read <addr 0x>:\r\n -- Expects 1 parameters\r\n",
	prvReadRegCommand, /* The function to run. */
	1
};

static const CLI_Command_Definition_t xWriteReg =
{
	"reg_write",
	"\r\nreg_write <addr 0x> <data 0x>:\r\n -- Expects 2 parameters\r\n",
	prvWriteRegCommand, /* The function to run. */
	2
};

static const CLI_Command_Definition_t xSpiTool =
{
	"spi_tool",
	"\r\nspi_tool <dev 0x> <data 0x>:\r\n 0x1 hmc7044 \r\n 0x2 dac \r\n 0x4 adc \r\n",
	prvSpiToolCommand, /* The function to run. */
	2
};

static const CLI_Command_Definition_t xMdioRead =
{
	"mdio_read",
	"\r\nmdio_read <0xPhyAddress> <0xRegisterNum>:\r\n -- mdio read\r\n",
	prvMdioReadCommand, /* The function to run. */
	2
};

static const CLI_Command_Definition_t xMdioWrite =
{
	"mdio_write",
	"\r\nmdio_write <0xPhyAddress> <0xRegisterNum> <PhyData>:\r\n -- mdio write\r\n",
	prvMdioWriteCommand, /* The function to run. */
	3
};


/*-----------------------------------------------------------*/

void vRegisterSampleCLICommands( void )
{
	/* Register all the command line commands defined immediately above. */
	FreeRTOS_CLIRegisterCommand( &xGwShow );	
	FreeRTOS_CLIRegisterCommand( &xReadReg );
	FreeRTOS_CLIRegisterCommand( &xWriteReg );
	FreeRTOS_CLIRegisterCommand( &xSpiTool );
	FreeRTOS_CLIRegisterCommand( &xMdioRead );
	FreeRTOS_CLIRegisterCommand( &xMdioWrite );

}
/*-----------------------------------------------------------*/

static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "The parameters were:\r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		if( pcParameter != NULL )
		{
			/* Return the parameter string. */
			memset( pcWriteBuffer, 0x00, xWriteBufferLen );
			sprintf( pcWriteBuffer, "%d: ", ( int ) uxParameterNumber );
			strncat( pcWriteBuffer, ( char * ) pcParameter, ( size_t ) xParameterStringLength );
			strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );

			/* There might be more parameters to return after this one. */
			xReturn = pdTRUE;
			uxParameterNumber++;
		}
		else
		{
			/* No more parameters were found.  Make sure the write buffer does
			not contain a valid string. */
			pcWriteBuffer[ 0 ] = 0x00;

			/* No more data to return. */
			xReturn = pdFALSE;

			/* Start over the next time this command is executed. */
			uxParameterNumber = 0;
		}
	}

	return xReturn;
}
/*-----------------------------------------------------------*/


/* -------------------------------- GT101 Command ------------------------------- */


static BaseType_t prvReadRegCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "prvReadRegCommand : \r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Return the parameter string. */
		memset( pcWriteBuffer, 0x00, xWriteBufferLen );
		// sprintf( pcWriteBuffer, "%d: ", ( int ) uxParameterNumber );
		// strncat( pcWriteBuffer, pcParameter, ( size_t ) xParameterStringLength );

		u32 address = stringToInt(pcParameter);
		u32 high16bit = valid_address(address);
		if(high16bit != 0){
			sprintf( pcWriteBuffer, "ReadReg - address Error: address = 0x%x : high16bit =0x%x ! \r\n", address, high16bit);
		}else{
			u32 value = gw_ReadReg(address);
			sprintf( pcWriteBuffer, "%d : %s , ReadReg - address : 0x%x , value = 0x%x \r\n", ( int ) uxParameterNumber, pcParameter, address, value);
		}

		if( uxParameterNumber == 1U )
		{
			xReturn = pdFALSE;
			uxParameterNumber = 0;
		}
	}

	return xReturn;
}

static BaseType_t prvWriteRegCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

static u32 value = 0x0;
static u32 address = 0x0;

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "prvWriteRegCommand : \r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Return the parameter string. */
		if(uxParameterNumber == 1U){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			address = stringToInt(tmp_str);
			uxParameterNumber = uxParameterNumber + 1;
			xReturn = pdPASS;
		}else if( uxParameterNumber == 2U ){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			value = stringToInt(tmp_str);

			memset( pcWriteBuffer, 0x00, xWriteBufferLen );
			{
				if(valid_address(address) != 0){
					sprintf( pcWriteBuffer, "%s , WriteReg - address Error ! \r\n", pcCommandString);
				}else{
					gw_WriteReg(address,value);
					sprintf( pcWriteBuffer, "%s , WriteReg - address : 0x%x , value = 0x%x \r\n", pcCommandString, address, value);
				}
			}
			xReturn = pdFALSE;
			uxParameterNumber = 0;
			value = 0x0;
			address = 0x0;
		}
	}

	return xReturn;
}

static BaseType_t prvSpiToolCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

static u32 data = 0x0;
static u32 slave_mask = 0x0;

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "prvSpiToolCommand : \r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Return the parameter string. */
		if(uxParameterNumber == 1U){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			slave_mask = stringToInt(tmp_str);
			uxParameterNumber = uxParameterNumber + 1;
			xReturn = pdPASS;
		}else if( uxParameterNumber == 2U ){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			data = stringToInt(tmp_str);
			u8 rx_data = 0x0;
			memset( pcWriteBuffer, 0x00, xWriteBufferLen );
			if( cli_spi_cmd(data, slave_mask, &rx_data) != 0){
				sprintf( pcWriteBuffer, "spi cmd error !\r\n" );
			}else{
				sprintf( pcWriteBuffer, "spi_tool 0x%x 0x%x : rx_value = 0x%x\r\n", slave_mask, data, rx_data);
			}
			xReturn = pdFALSE;
			uxParameterNumber = 0;
			data = 0x0;
			slave_mask = 0x0;
		}
	}

	return xReturn;
}

static BaseType_t prvGwShowCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	memset( pcWriteBuffer, 0x00, xWriteBufferLen );
	cli_show(pcWriteBuffer);

	/* There is no more data to return after this single string, so return
	pdFALSE. */
	return pdFALSE;
}

static BaseType_t prvMdioReadCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

static u32 PhyAddress = 0x0; // PhyAddress -- value
static u32 RegisterNum = 0x0; // RegisterNum -- address

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "prvMdioReadCommand : \r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Return the parameter string. */
		if(uxParameterNumber == 1U){
			u8 tmp_str[16];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			PhyAddress = stringToInt(tmp_str);
			uxParameterNumber = uxParameterNumber + 1;
			xReturn = pdPASS;
		}else if( uxParameterNumber == 2U ){
			u8 tmp_str[16];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			RegisterNum = stringToInt(tmp_str);

			memset( pcWriteBuffer, 0x00, xWriteBufferLen );
			{
				u16 value = TriMode_PhyRead(PhyAddress, RegisterNum);
				sprintf( pcWriteBuffer, "%s , MdioRead - PhyAddress : 0x%x , RegisterNum = 0x%x , value = 0x%x \r\n", pcCommandString, PhyAddress, RegisterNum, value);
			}
			xReturn = pdFALSE;
			uxParameterNumber = 0;
			PhyAddress = 0x0;
			RegisterNum = 0x0;
		}
	}

	return xReturn;
}

static BaseType_t prvMdioWriteCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
const char *pcParameter;
BaseType_t xParameterStringLength, xReturn;
static UBaseType_t uxParameterNumber = 0;

static u32 PhyAddress = 0x0; 
static u32 RegisterNum = 0x0;
static u32 PhyData = 0x0;
// <0xPhyAddress> <0xRegisterNum> <PhyData>

	/* Remove compile time warnings about unused parameters, and check the
	write buffer is not NULL.  NOTE - for simplicity, this example assumes the
	write buffer length is adequate, so does not check for buffer overflows. */
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );

	if( uxParameterNumber == 0 )
	{
		/* The first time the function is called after the command has been
		entered just a header string is returned. */
		sprintf( pcWriteBuffer, "prvMdioWriteCommand : \r\n" );

		/* Next time the function is called the first parameter will be echoed
		back. */
		uxParameterNumber = 1U;

		/* There is more data to be returned as no parameters have been echoed
		back yet. */
		xReturn = pdPASS;
	}
	else
	{
		/* Obtain the parameter string. */
		pcParameter = FreeRTOS_CLIGetParameter
						(
							pcCommandString,		/* The command string itself. */
							uxParameterNumber,		/* Return the next parameter. */
							&xParameterStringLength	/* Store the parameter string length. */
						);

		/* Sanity check something was returned. */
		configASSERT( pcParameter );

		/* Return the parameter string. */
		if(uxParameterNumber == 1U){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			PhyAddress = stringToInt(tmp_str);
			uxParameterNumber = uxParameterNumber + 1;
			xReturn = pdPASS;
		}else if( uxParameterNumber == 2U ){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			RegisterNum = stringToInt(tmp_str);
			uxParameterNumber = uxParameterNumber + 1;
			xReturn = pdPASS;
		}else if( uxParameterNumber == 3U ){
			u8 tmp_str[10];
			memcpy(tmp_str, pcParameter, xParameterStringLength);
			tmp_str[xParameterStringLength] = '\0';
			PhyData = stringToInt(tmp_str);

			memset( pcWriteBuffer, 0x00, xWriteBufferLen );
			{
				TriMode_PhyWrite(PhyAddress, RegisterNum, (u16)PhyData);
				sprintf( pcWriteBuffer, "%s , MdioWrite - PhyAddress : 0x%x , RegisterNum = 0x%x , PhyData = 0x%x \r\n", pcCommandString, PhyAddress, RegisterNum, PhyData);
			}
			xReturn = pdFALSE;
			uxParameterNumber = 0;
			PhyAddress = 0x0;
			RegisterNum = 0x0;
			PhyData = 0x0;
		}
	}

	return xReturn;
}



